package com.tarena.demo.luban.all.main.service.impl;

import com.tarena.demo.luban.all.main.mapper.CartMapper;
import com.tarena.demo.luban.all.main.mapper.OrderMapper;
import com.tarena.demo.luban.all.main.service.ICartService;
import com.tarena.demo.luban.all.main.service.IOrderService;
import com.tarena.demo.luban.all.main.service.IStockService;
import com.tarena.demo.luban.protocol.cart.param.CartDeleteParam;
import com.tarena.demo.luban.protocol.order.dos.OrderDO;
import com.tarena.demo.luban.protocol.order.param.OrderAddParam;
import com.tarena.demo.luban.protocol.stock.dos.StockDO;
import com.tarena.demo.luban.protocol.stock.param.StockReduceCountParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    ICartService cartService;
    @Autowired
    IStockService stockService;
    @Override
    public void addOrder(OrderAddParam orderAddParam) {
        StockReduceCountParam stockReduceCountParam = new StockReduceCountParam();
        stockReduceCountParam.setReduceCount(orderAddParam.getCount());
        stockReduceCountParam.setProductCode(orderAddParam.getProductCode());
        stockService.update(stockReduceCountParam);

        // 新增订单
        OrderDO orderDO = new OrderDO();
        BeanUtils.copyProperties(orderAddParam,orderDO);
        orderMapper.addOrder(orderDO);

        // 删除购物车
        CartDeleteParam cartDeleteParam = new CartDeleteParam();
        cartDeleteParam.setUserId(orderAddParam.getUserId());
        cartDeleteParam.setProductCode(orderAddParam.getProductCode());
        cartService.deleteCart(cartDeleteParam);

    }
}
